package com.gitee.ty850454.leetcode1047;

public class Leetcode1047_2 {
    public static void main(String[] args) {
        String result;
        result = new Leetcode1047_2().removeDuplicates("aaaaaaaa");
        System.out.println(result);
        result = new Leetcode1047_2().removeDuplicates("abbaca");
        System.out.println(result);
    }

    public String removeDuplicates(String s) {
        char[] source = s.toCharArray();
        char[] target = new char[s.length()];
        int targetEndIndex = -1;
        for (char c : source) {
            if (targetEndIndex >= 0 && target[targetEndIndex] == c) {
                targetEndIndex--;
            } else {
                target[++targetEndIndex] = c;
            }
        }
        return new String(target, 0, targetEndIndex + 1);
    }
}
